package com.dchh.android.suverynew.common;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;

import com.dchh.android.suverynew.R;
import com.dhcc.android.base.common.view.MyPopupWindow;
import com.dhcc.android.base.common.view.ZoomImageView;
import com.dhcc.android.base.utils.PhotoUtil;

/**
 * 图片的放大缩小弹窗
 *
 * @author pengling
 * @date 2015-5-29
 */
@SuppressLint("ResourceAsColor")
public class ZoomImagePoup {
    private MyPopupWindow popupwindow;
    private Activity activity;
    private ZoomImageView view;

    public ZoomImagePoup(Activity activity) {
        this.activity = activity;
    }

    public void initPopWindow(View parent, Bitmap bitmap) {

        if (popupwindow == null) {
            view = new ZoomImageView(activity);
            view.setImageBitmap(bitmap);
            view.setBackgroundColor(R.color.black);
            popupwindow = new MyPopupWindow(view, LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT, true);
            popupwindow.setAnimationStyle(R.style.PopupCenterAnimation);
        }
//		ColorDrawable dw = new ColorDrawable(000);//-00000
//		popupwindow.setBackgroundDrawable(dw);
        popupwindow.update();
        popupwindow.showAtLocation(parent, Gravity.CENTER | Gravity.CENTER, 0,
                0);// 显示在最中央的位置
        view.setClickable(true);
//		view.setOnClickListener(itemsOnClickBtn);
    }

    public void dismiss() {
        if (bitmap != null) {
            if (!bitmap.isRecycled()) {
                bitmap.recycle(); // 回收图片所占的内存
                bitmap = null;
                System.gc(); // 提醒系统及时回收
            }
        }
        if (popupwindow != null) {
            popupwindow.dismiss();
        }
    }

    // 为弹出窗口实现监听类
    private OnClickListener itemsOnClickBtn = new OnClickListener() {
        public void onClick(View v) {
            System.out.println("点击我了");
            dismiss();
        }
    };


    //===========================================================================
    private Bitmap bitmap;

    public void show(View parent, String path, PhotoUtil photoUtil) {

        if (popupwindow == null) {
            view = new ZoomImageView(activity);
            bitmap = photoUtil.loadBitmapImage(path);
            view.setImageBitmap(bitmap);
            view.setBackgroundColor(R.color.black);
            popupwindow = new MyPopupWindow(view, LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT, true);
            popupwindow.setAnimationStyle(R.style.PopupCenterAnimation);
        }
        popupwindow.update();
        popupwindow.showAtLocation(parent, Gravity.CENTER | Gravity.CENTER, 0, 0);// 显示在最中央的位置
        view.setClickable(true);
        view.setOnClickListener(itemsOnClickBtn);

    }
}
